﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;
using Model;
using System.Collections;
using Controls;
using UI.Common;
using UI.Lib.Factory;
using Components;

namespace UI
{
    public class PopHelp
    {
        public static UI.Common.PopItem GetPopItem(string popId)
        {
            UI.Common.PopItem dPopItem = CommonHelper.Entity.GetPopItem(popId); //得到PopItem对象

            if (null == dPopItem)
            {
                LogHelper.Entity.Out(string.Format("未弹出框设置数据源（{0}）", popId));
                throw new Exception("请设置弹出框的相关属性!");
            }

            return dPopItem;
        }

        /// <summary>
        /// 根据sqlId得到Page对象
        /// </summary>
        /// <param name="sqlId"></param>
        /// <returns></returns>
        public static UI.Common.PopItem GetPopItem(string popId, UI.Common.PopItem pop, Hashtable h)
        {
            UI.Common.PopItem dPopItem = CommonHelper.Entity.GetPopItem(popId); //得到PopItem对象

            if (null == dPopItem)
            {
                LogHelper.Entity.Out(string.Format("未弹出框设置数据源（{0}）", popId));
                throw new Exception("请设置弹出框的相关属性!");
            }

            if (pop == null)
                pop = new UI.Common.PopItem();

            pop.Table = PopHelp.UpdateModel("table", dPopItem.Table, h);
            if (string.IsNullOrEmpty(pop.Table))
                throw new Exception("表不能为空!");


            pop.Key = PopHelp.UpdateModel("key", dPopItem.KeyRef, h);
            if (string.IsNullOrEmpty(pop.Key))
                throw new Exception("key不能为空!");

            pop.ShowFields = PopHelp.UpdateModel("showfields", dPopItem.ShowFields, h);
            if (string.IsNullOrEmpty(pop.ShowFields)) //
                throw new Exception("显示的字读不能为空!");

            pop.PageSize = (0 == dPopItem.PageSize) ? 10 : pop.PageSize;//默认是10

            string OrderFid = PopHelp.UpdateModel("orderfid", dPopItem.OrderFid, h);
            if (!string.IsNullOrEmpty(OrderFid))
                pop.OrderFid = OrderFid;

            string strWhere = PopHelp.UpdateModel("strwhere", dPopItem.StrWhere, h);
            if (!string.IsNullOrEmpty(strWhere))
                pop.StrWhere = strWhere;

            string refField = PopHelp.UpdateModel("reffield", dPopItem.RefField, h);
            if (!string.IsNullOrEmpty(refField))
                pop.RefField = refField;

            string description = PopHelp.UpdateModel("description", dPopItem.Desc, h);
            if (!string.IsNullOrEmpty(description))
                pop.Desc = description;

            string keyRef = PopHelp.UpdateModel("keyref", dPopItem.KeyRef, h);
            if (!string.IsNullOrEmpty(keyRef))
                pop.KeyRef = keyRef;

            //pop.MultiSelect = PopHelp.UpdateModel("multiselect", xml.SelectNodeText(string.Format(searchkey, "MultiSelect")), h);

            pop.IsSaveKey = dPopItem.IsSaveKey;

            pop.Width = dPopItem.Width;

            pop.Height = dPopItem.Height;

            return pop;
        }


        /// <summary>
        /// 根据子页面传入的条件修改数据源
        /// </summary>
        /// <param name="key">更新的键名</param>
        /// <param name="xmlValue">XML中该项的值</param>
        /// <param name="h">传入的修改值的Hashtable</param>
        /// <returns></returns>
        private static string UpdateModel(string key, string xmlValue, Hashtable h)
        {
            key = key.ToLower();

            if (h == null || h.Count == 0 || !h.ContainsKey(key))
                return xmlValue;

            string modifyValue = h[key].ToString(); //取出修改的值

            return modifyValue.Replace("+=", xmlValue);//把+=替换成XML中设置的值，实现追加的效果
        }

        /// <summary>
        /// 弹出框弹出后将值赋值到各个文本框中
        /// </summary>
        /// <param name="ctls"></param>
        /// <param name="popTarget"></param>
        /// <param name="hs"></param>
        /// <param name="clearFlag"></param>
        public static void AfterPopWin(System.Windows.Forms.Control.ControlCollection ctls, PsPopWin popTarget, Hashtable hs, bool clearFlag, string status)
        {
            if (null == hs || 0 == hs.Count) return;

            Control c = null;
            PsPopWin pop = null;
            string key = "";
            string value = "";
            bool isAlert = false;
            foreach (DictionaryEntry entry in hs)
            {
                key = entry.Key.ToString();
                value = (null == entry.Value || clearFlag) ? "" : entry.Value.ToString();
                if (key == "$SELF$")
                {
                    popTarget.Text = popTarget.Value = value;
                    if (!clearFlag && string.IsNullOrEmpty(value))
                    {
                        isAlert = true; //给出提示
                    }
                }
                else if (key.IndexOf("$VALUE$") > -1)//准备将值写入弹出框份额Value内
                {
                    if (key.IndexOf("&") > -1)//准备将值写入其他弹出框份额Value内
                    {
                        string name = key.Replace("$VALUE$&", "");
                        if (!ctls.ContainsKey(name)) continue;

                        pop = ctls[name] as PsPopWin;
                        if (pop == c) continue;

                        pop.Value = value;
                    }
                    else
                    {
                        popTarget.Value = value;
                    }
                }
                else
                {
                    if (!ctls.ContainsKey(key)) continue;

                    c = ctls[key];
                    if (null == c) continue;

                    SetValue(c, value);
                }
            }

            if (isAlert && "M" != status)
            {
                MessageBox.Show(string.Format("[{0}]未选择有效的值！", popTarget.Lable), "提示");
            }
        }

        public static void AfterGridPopWin(DataGridViewRow dr, string t, Hashtable hs, string status)
        {
            AfterGridPopWin(dr, t, hs, false, status);
        }

        public static void AfterGridPopWin(DataGridViewRow dr, string t, Hashtable hs, bool isEmptyData, string status)
        {
            if (null == hs || 0 == hs.Count) return;

            DataGridViewColumnCollection dcc = dr.DataGridView.Columns;
            bool isAlert = false;
            string val = "";
            foreach (DictionaryEntry entry in hs)
            {
                if (entry.Key.ToString().ToUpper() == "$SELF$")
                {
                    if (!dcc.Contains(t)) continue;
                    val = ((null == entry.Value || isEmptyData) ? "" : entry.Value.ToString());
                    dr.Cells[t].Value = val;

                    if (!isEmptyData && string.IsNullOrEmpty(val))
                    {
                        isAlert = true; //给出提示
                    }
                }
                else
                {
                    if (!dcc.Contains(entry.Key.ToString())) continue;

                    dr.Cells[entry.Key.ToString()].Value = ((null == entry.Value || isEmptyData) ? "" : entry.Value.ToString());
                }
            }

            if (isAlert && "M" != status)
            {
                //MessageBox.Show("未选择有效的值！", "提示");
            }
        }

        public static void SetValue(Control c, string v)
        {
            if (c is PsTextBox || c is PsPopWin)
            {
                TextBox ptb = c as TextBox;
                if (null == ptb) return;

                ptb.Text = v;
            }
            else if (c is PsDatePicker)
            {
                PsDatePicker pdp = c as PsDatePicker;
                if (null == pdp) return;

                pdp.Text = v;
            }
        }

    }
}
